<?php
require_once('inc/sync.php');

class Air_Port {

    private static $_instance;

    /**
     * 
     * Create connection database
     * @return Mysql
     * 
     */
    public static function factory() {
        if (!self::$_instance) {
            self::$_instance = new Mysql();
        }
        return self::$_instance;
    }
    
    public static function getId() {
        return self::factory()->getId();
    }
	
    /**
     * 
     * @param type $param
     * 
     */
    public static function findLichBayDi($param) {
        $arr_go = explode("(", $param["go"]);
        $arr_to = explode("(", $param["to"]);
        $year = getdate();

        $current_year = $year["year"];
        $day_go = $param["day_go"];
        $day_back = $param["day_back"] === "0" ? date("d", time()) : $param["day_back"];
        $month_go = $param["month_go"];
        $month_back = $param["month_back"] === "0" ? date("m", time()) : $param["month_back"];

        $stamp_go = mktime(0, 0, 0, $month_go, $day_go, $current_year);
        $stamp_back = mktime(0, 0, 0, $month_back, $day_back, $current_year);

        if (isset($arr_go[1])) {
            $go = substr($arr_go[1], 0, strlen(trim($arr_go[1])) - 1);
            $query_go = "SELECT * FROM airport_code WHERE code LIKE '%{$go}%'";
            $rs_go = self::factory()->find($query_go);
            foreach ($rs_go as $_rs_go) {
                $go = $_rs_go["id"];
            }
        } else {
            $query_go = "SELECT * FROM airport_code WHERE title LIKE '%{$param["go"]}%' OR code LIKE '%{$param["go"]}%'";
            $rs_go = self::factory()->find($query_go);
            foreach ($rs_go as $_rs_go) {
                $go = $_rs_go["id"];
            }
        }

        if (isset($arr_to[1])) {
            $to = substr($arr_to[1], 0, strlen(trim($arr_to[1])) - 1);
            $query_to = "SELECT * FROM airport_code WHERE code LIKE '%{$to}%'";
            $rs_to = self::factory()->find($query_to);
            foreach ($rs_to as $_rs_to) {
                $to = $_rs_to["id"];
            }
        } else {
            $query_to = "SELECT * FROM airport_code WHERE title LIKE '%{$param["to"]}%' OR code LIKE '%{$param["to"]}%'";
            $rs_to = self::factory()->find($query_to);
            foreach ($rs_to as $_rs_to) {
                $to = $_rs_to["id"];
            }
        }

        $query = "SELECT * FROM flight WHERE airport_code_go = '{$go}' and airport_code_to = '{$to}' and ngay_di >= {$stamp_go} and ngay_den <= {$stamp_back}";
        return self::factory()->find($query);
    }
    
    /**
     * 
     * @param type $param
     * 
     */
    public static function findLichBayVe($param) {
        $arr_go = explode("(", $param["go"]);
        $arr_to = explode("(", $param["to"]);
        $year = getdate();

        $current_year = $year["year"];
        $day_go = $param["day_go"];
        $day_back = $param["day_back"] === "0" ? date("d", time()) : $param["day_back"];
        $month_go = $param["month_go"];
        $month_back = $param["month_back"] === "0" ? date("m", time()) : $param["month_back"];

        $stamp_go = mktime(0, 0, 0, $month_go, $day_go, $current_year);
        $stamp_back = mktime(0, 0, 0, $month_back, $day_back, $current_year);

        if (isset($arr_go[1])) {
            $go = substr($arr_go[1], 0, strlen(trim($arr_go[1])) - 1);
            $query_go = "SELECT * FROM airport_code WHERE code LIKE '%{$go}%'";
            $rs_go = self::factory()->find($query_go);
            foreach ($rs_go as $_rs_go) {
                $go = $_rs_go["id"];
            }
        } else {
            $query_go = "SELECT * FROM airport_code WHERE title LIKE '%{$param["go"]}%' OR code LIKE '%{$param["go"]}%'";
            $rs_go = self::factory()->find($query_go);
            foreach ($rs_go as $_rs_go) {
                $go = $_rs_go["id"];
            }
        }

        if (isset($arr_to[1])) {
            $to = substr($arr_to[1], 0, strlen(trim($arr_to[1])) - 1);
            $query_to = "SELECT * FROM airport_code WHERE code LIKE '%{$to}%'";
            $rs_to = self::factory()->find($query_to);
            foreach ($rs_to as $_rs_to) {
                $to = $_rs_to["id"];
            }
        } else {
            $query_to = "SELECT * FROM airport_code WHERE title LIKE '%{$param["to"]}%' OR code LIKE '%{$param["to"]}%'";
            $rs_to = self::factory()->find($query_to);
            foreach ($rs_to as $_rs_to) {
                $to = $_rs_to["id"];
            }
        }

        $query = "SELECT * FROM flight WHERE airport_code_go = '{$to}' and airport_code_to = '{$go}' and ngay_di >= {$stamp_back}";
        return self::factory()->find($query);
    }
    
    public static function findOrderById($param) {
        $query = "SELECT * FROM orders WHERE id = '{$param["order_code"]}'";
        return self::factory()->find($query);
    }

    public static function addContact($param) {
        $arr = array(
            "title" => $param["name"],
            "address" => $param["address"],
            "phone" => $param["phone"],
            "email" => $param["email"],
            "content" => $param["comment"],
            "time" => time(),
            "status" => 0,
        );
        return self::factory()->insert("contact", $arr);
    }

    public static function findByLichBayId($id) {
        $query = "SELECT * FROM flight WHERE id='{$id}'";
        return self::factory()->find($query);
    }
    
    public static function findByLichBayAirCode($air_code) {
        $query = "SELECT * FROM flight WHERE airport_code_go='{$air_code}'";
        return self::factory()->find($query);
    }
    
    public static function addOrder($param) {
        $check = $param["IsInvoice"] === "true" ? "1" : "0";
        
        $arr = array(
            "ngay_di" => $param["ngay_di"],
            "ngay_ve" => $param["ngay_ve"],
            "chuyen_bay_di" => $param["ma_chuyen_bay"],
            "chuyen_bay_ve" => $param["ma_chuyen_bay_ve"],
            "ten_nguoi_nhan" => $param["fullname"],
            "dia_chi_nguoi_nhan" => $param["address"],
            "email" => $param["email"],
            "phone" => $param["phone"],
            "city" => $param["city"],
            "country" => $param["country"],
            "hoa_don" => $check,
            "ten_cong_ty" => $param["nameorder"],
            "ma_so_thue" => $param["tax"],
            "dia_chi_nhan_hoa_don" => $param["InvoiceAddress"],
            "dia_chi_cong_ty" => $param["addressorder"],
            "yeu_cau" => $param["txtRemark"],
            "time" => time(),
            "status" => 0
        );
        return self::factory()->insert("orders", $arr);
    }

    public static function findAirportCodeId($id){
        $query = "SELECT * FROM airport_code WHERE id={$id}";
        return self::factory()->find($query);
    }
    
    public static function findAirportByCodeId($id){
        $query = "SELECT * FROM airport WHERE airport_code_id={$id}";
        return self::factory()->find($query);
    }
    
    public static function findAirLineById($id){
        $query = "SELECT * FROM airlines WHERE id={$id}";
        return self::factory()->find($query);
    }
}

?>
